package lainey.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;

import lainey.ui.dialog.LoadingDialog;
import lainey.ui.utils.NetworkUtil;
import lainey.ui.utils.ThreadPoolManager;

public class BaseFragment extends Fragment implements View.OnClickListener {

    protected Context mContext;
    protected Activity mActivity;

    protected LoadingDialog mLoadingDialog;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getContext();
        mActivity = getActivity();
    }

    @Override
    public void onClick(View v) {

    }

    public void onNetworkError() {
        dismissLoadingDialogOnUi();
        showToastOnUi(R.string.ui_hint_network);
    }

    public void openActivity(Class clazz) {
        Activity activity = getActivity();
        if (activity != null) {
            Intent intent = new Intent(activity, clazz);
            activity.startActivity(intent);
        }
    }

    public void runOnUi(Runnable runnable) {
        if (mActivity == null) return;
        mActivity.runOnUiThread(runnable);
    }

    public void showLoadingDialogOnUi() {
        showLoadingDialogOnUi(null);
    }

    public void showLoadingDialogOnUi(int resId) {
        String text = getString(resId);
        showLoadingDialogOnUi(text);
    }

    public void showLoadingDialogOnUi(final String message) {
        runOnUi(
                () -> {
                    try {
                        getLoadingDialog(message);
                        mLoadingDialog.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        );
    }

    public void dismissLoadingDialogOnUi() {
        runOnUi(
                () -> {
                    try {
                        if (mLoadingDialog != null) {
                            mLoadingDialog.dismiss();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        );
    }

    private void getLoadingDialog(String message) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mActivity, message);
        } else {
            mLoadingDialog.setMessage(message);
        }
    }

    public void executeTask(Runnable runnable) {
        boolean connected = NetworkUtil.isNetworkConnected(mContext);
        if (connected) {
            ThreadPoolManager.executeInCachePool(runnable);
        } else {
            onNetworkError();
        }
    }

    public void showToastOnUi(int resId) {
        runOnUi(
                () -> Toast.makeText(mActivity, resId, Toast.LENGTH_SHORT).show()
        );
    }

    public void showToastOnUi(String text) {
        runOnUi(
                () -> Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show()
        );
    }

    public void showToast(int resId) {
        Toast.makeText(mActivity, resId, Toast.LENGTH_SHORT).show();
    }

    public void showToast(String text) {
        Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
    }

}
